﻿using K9Nano.Dependency;
using K9Nano.Domain.Authorization;
using K9Nano.Session;
using Microsoft.AspNetCore.Identity;

namespace K9Nano.Application.Frontend;

internal class DefaultFrontendAppService(IUserContextAccessor userContextAccessor) : IFrontendAppService, IScopedDependency
{
    /// <summary>
    /// 获取当前用户可用路由
    /// </summary>
    /// <returns></returns>
    public IEnumerable<string> GetRoutes()
    {
        var user = userContextAccessor.CurrentUser;
        if (user is null) return [];

        var result = new List<string>();
        foreach (var item in FrontendRoutes.Routes)
        {
            if (string.IsNullOrEmpty(item.Value))
            {
                // 没有权限控制
                result.Add(item.Key);
                continue;
            }

            if (user.HasAnyPermission(item.Key))
            {
                result.Add(item.Key);
            }
        }

        return result;
    }
}